package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerContactAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerContact;
import com.isunland.managesystem.entity.CustomerContactOriginal;
import com.isunland.managesystem.entity.CustomerNeed;
import com.isunland.managesystem.utils.MyUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CustomerContactListFragment extends BaseListFragment {

  private ArrayList<CustomerContact> mCustomerContacts;
  private CustomerContactAdapter mAdapter;
  private String mCustomerNeedId;
  private CustomerNeed mCustomerNeed;

  public static final String EXTRA_CUSTOMER_ID = "com.isunland.managesystem.ui.CUSTOMER_ID";
  public static final String EXTRA_CUSTOMER_NEED =
      "com.isunland.managesystem.ui.CustomerContactListFragment.EXTRA_CUSTOMER_NEED";
  protected static final String TAG = "CustomerContactListFragment";
  private static final int REQUEST_CONTACT = 3;

  @Override public void onCreate(Bundle savedInstanceState) {

    //		mCustomerNeedId = getArguments().getString(EXTRA_CUSTOMER_ID);
    mCustomerNeed = (CustomerNeed) getArguments().getSerializable(EXTRA_CUSTOMER_NEED);
    mCustomerNeedId = mCustomerNeed.getId();
    super.onCreate(savedInstanceState);
  }

  public void onListItemClick(ListView l, View v, int position, long id) {
    Intent i = new Intent(getActivity(), CustomerContactActivity.class);
    CustomerContact customerContact =
        ((CustomerContactAdapter) getListAdapter()).getItem(position - 1);
    i.putExtra(CustomerContactFragment.EXTRA_CUSTOMER_CONTACT, customerContact);
    i.putExtra(CustomerContactFragment.EXTRA_TYPE, CustomerContactFragment.VALUE_SHOW);
    i.putExtra(CustomerContactFragment.EXTRA_CUSTOMER_NEED, mCustomerNeed);
    startActivityForResult(i, REQUEST_CONTACT);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_add, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case R.id.menu_item_add:
        Intent i = new Intent(getActivity(), CustomerContactActivity.class);

        i.putExtra(CustomerContactFragment.EXTRA_TYPE, CustomerContactFragment.VALUE_ADD);
        i.putExtra(CustomerContactFragment.EXTRA_NEED_ID, mCustomerNeedId);
        i.putExtra(CustomerContactFragment.EXTRA_CUSTOMER_NEED, mCustomerNeed);
        startActivityForResult(i, REQUEST_CONTACT);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    // Auto-generated method stub
    return ApiConst.URL_CUSTOMER_CONTACT_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("customerNeedId", mCustomerNeedId);
    return params;
  }

  public void resolveResponse(String arg0) {

    CustomerContactOriginal customerContactOriginal =
        new Gson().fromJson(arg0, CustomerContactOriginal.class);

    List<CustomerContact> customerContacts = customerContactOriginal.getRows();

    setAdapter(customerContacts);
    setListShown(true);
  }

  private void setAdapter(List<CustomerContact> customerContacts) {
    if (mCustomerContacts == null) {
      mCustomerContacts = new ArrayList<>();
    }
    mCustomerContacts.clear();
    mCustomerContacts.addAll(customerContacts);

    if (mAdapter == null) {
      mAdapter = new CustomerContactAdapter(getActivity(), mCustomerContacts);
      setListAdapter(mAdapter);
    }
    ((CustomerContactAdapter) getListAdapter()).notifyDataSetChanged();

    MyUtils.dismissDialog();
  }

  public static CustomerContactListFragment newInstance(String id) {
    Bundle argsBundle = new Bundle();
    argsBundle.putSerializable(EXTRA_CUSTOMER_ID, id);
    CustomerContactListFragment fragment = new CustomerContactListFragment();
    fragment.setArguments(argsBundle);
    return fragment;
  }

  public static CustomerContactListFragment newInstance(CustomerNeed customerNeed) {
    Bundle argsBundle = new Bundle();
    argsBundle.putSerializable(EXTRA_CUSTOMER_NEED, customerNeed);
    CustomerContactListFragment fragment = new CustomerContactListFragment();
    fragment.setArguments(argsBundle);
    return fragment;
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {

      return;
    }
    volleyPost();

    super.onActivityResult(requestCode, resultCode, data);
  }
}
